package com.mjh.zhujianju.controller.party;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.model.base.UserExtendEO;
import com.mjh.zhujianju.model.party.BuildingEventUsersEO;
import com.mjh.zhujianju.service.base.UserExtendService;
import com.mjh.zhujianju.service.party.BuildingEventUsersService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author liuyang
 * @Description: 活动
 * @date 2018/8/16 23:52
 */
@RestController
@RequestMapping("/event")
@Api("WX—>党建—>活动 ")
public class BuildingEventController {

    @Autowired
    private UserExtendService userExtendService;

    @Autowired
    private BuildingEventUsersService buildingEventUsersService;

    @ApiOperation(value="立即报名", notes="活动报名，只能党员能参加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventId", value = "活动id", required = true, dataType = "Long", paramType = "path")
    })
    @RequestMapping(value = "/joinEvent/{eventId}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> add (@PathVariable(value = "eventId")Long eventId){
        JsonResult result = new JsonResult();
        try {

            //参数验证
            if (eventId == null){
                result.setResult("参数异常");
                result.setStatus(ApiConstant.MSG_PARAMETER);
                return ResponseEntity.ok(result);
            }

            Long userId = XTCurrentUser.getCurrentUserId();
           if (null!=userId){
               //党员验证
               UserExtendEO userExtendEO = userExtendService.queryUserExtendByUserId(userId);
               if (userExtendEO==null || !userExtendEO.getIsParty().equals("1")){
                   result.setResult("不是党员");
                   result.setStatus(ApiConstant.MSG_NO_PARTY);
                   return ResponseEntity.ok(result);
               }

               //保存
               BuildingEventUsersEO buildingEventUsersEO = new BuildingEventUsersEO();
               buildingEventUsersEO.setEventId(eventId);
               buildingEventUsersEO.setUserId(userId);
               buildingEventUsersService.addJoinEvent(buildingEventUsersEO);
               result.setStatus(ApiConstant.MSG_SUCCESS);
           }else {
               result.setResult("请先登录");
               result.setStatus(ApiConstant.MSG_NO_USER);
               return ResponseEntity.ok(result);
           }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }
}
